package cn.aib.eduservice.service.impl;

import cn.aib.eduservice.entity.EduChapter;
import cn.aib.eduservice.entity.EduVideo;
import cn.aib.eduservice.entity.chapter.ChapterVo;
import cn.aib.eduservice.entity.chapter.VideoVo;
import cn.aib.eduservice.mapper.EduChapterMapper;
import cn.aib.eduservice.service.EduChapterService;
import cn.aib.eduservice.service.EduVideoService;
import cn.aib.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    @Override
    //返回章节和小节数据
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //查询对应课程ID所有的章节
        //设置查询条件
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        //执行查询，返回章节数据
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);

        //查询对应课程ID所有的小结
        //设置查询条件
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        //执行查询，返回小节数据
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);

        //遍历章节，进行数据转换，并遍历小节加以判断加入到章节的List小节中
        //创建要返回的List<ChapterVo>
        List<ChapterVo> chapterVoList = new ArrayList<>();
        //遍历章节
        for (int i = 0; i < eduChapterList.size(); i++) {
            //创建ChapterVo
            ChapterVo chapterVo = new ChapterVo();

            //拿到当前的章节
            EduChapter eduChapter = eduChapterList.get(i);

            //数据复制
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //遍历小节
            List<VideoVo> videoVoList = new ArrayList<>();

            for (int m = 0; m < eduVideoList.size(); m++) {
                //得到当前的小节节点
                EduVideo eduVideo = eduVideoList.get(m);

                //判断小节节点是否属于当前章节
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    //数据复制
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //是就设置进去
                    videoVoList.add(videoVo);
                }
            }
            //设置小节列表数据
            chapterVo.setChildren(videoVoList);

            //设置到chapterVoList中
            chapterVoList.add(chapterVo);
        }

        return chapterVoList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();

        wrapper.eq("chapter_id", chapterId);

        //根据章节ID查询小节的数量
        int count = eduVideoService.count(wrapper);

        //如果数量大于0，代表章节下有小节，不删除，抛出异常
        if(count>0){
            throw new GuliException(20001, "章节删除失败,还有小节在内");
        }else {
            //数量等于0就删除
            int result = baseMapper.deleteById(chapterId);

            return result>0;
        }
    }

    @Override
    public void removeChapterList(String courseId) {
        //删除章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();

        chapterQueryWrapper.eq("course_id", courseId);

        baseMapper.delete(chapterQueryWrapper);
    }
}
